package com.example.milimili;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import com.aigestudio.wheelpicker.WheelPicker;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class SearchConstraintActivity extends AppCompatActivity {

    private Button startBtn,endBtn,categoryBtn,cancelBtn,confirmBtn;
    public static TextView startText,endText,categoryText;
    //    private String[] categories=new String[]{"娱乐","军事","教育","文化","健康","财经","体育","汽车","科技","社会"};
    private List<String> categories=new ArrayList<>();

    private String startDate="";
    private String category="";
    private String endDate="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_constraint);
        initView();
    }

    private void initView() {
        categories.add("娱乐");
        categories.add("军事");
        categories.add("教育");
        categories.add("文化");
        categories.add("健康");
        categories.add("财经");
        categories.add("体育");
        categories.add("汽车");
        categories.add("科技");
        categories.add("社会");
        startBtn=findViewById(R.id.startChoose);
        endBtn=findViewById(R.id.endChoose);
        categoryBtn=findViewById(R.id.categoryChoose);
        startText=findViewById(R.id.startShow);
        endText=findViewById(R.id.endShow);
        categoryText=findViewById(R.id.categoryShow);
        cancelBtn=findViewById(R.id.cancel);
        confirmBtn=findViewById(R.id.confirm);
        startText.setText("");
        endText.setText("");
        categoryText.setText("");
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar calendar=Calendar.getInstance();
                new DatePickerDialog(SearchConstraintActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                        DecimalFormat g1=new DecimalFormat("00");
                        String month0=g1.format(Integer.valueOf(month)+1);
                        String day0=g1.format(Integer.valueOf(dayOfMonth));
                        String text =year + "-" + month0 + "-" + day0;
                        startDate=text;
                        startText.setText(text);
                    }
                },calendar.get(Calendar.YEAR)
                        ,calendar.get(Calendar.MONTH)
                        ,calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
        endBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar calendar=Calendar.getInstance();
                new DatePickerDialog(SearchConstraintActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                        DecimalFormat g1=new DecimalFormat("00");
                        String month0=g1.format(Integer.valueOf(month)+1);
                        String day0=g1.format(Integer.valueOf(dayOfMonth));
                        String text =year + "-" + month0 + "-" + day0;
                        endDate=text;
                        endText.setText(text);
                    }
                },calendar.get(Calendar.YEAR)
                        ,calendar.get(Calendar.MONTH)
                        ,calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
        categoryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder=new AlertDialog.Builder(SearchConstraintActivity.this);
                View wheelView=View.inflate(SearchConstraintActivity.this,R.layout.wheelchooser,null);
                WheelPicker wheelPicker=wheelView.findViewById(R.id.wheel);
                wheelPicker.setData(categories);
                wheelPicker.setIndicator(true);
                wheelPicker.setCurtain(true);
                wheelPicker.setAtmospheric(true);
                builder.setTitle("Choose the category");
                builder.setView(wheelView);     //选择布局文件
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        categoryText.setText(categories.get(wheelPicker.getCurrentItemPosition()));
                        category=categories.get(wheelPicker.getCurrentItemPosition());
                        dialogInterface.dismiss();
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                AlertDialog myDialog=builder.create();
                myDialog.show();
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SearchConstraintActivity.this.finish();
            }
        });
        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SearchActivity.startDate=startDate;
                SearchActivity.endDate=endDate;
                SearchActivity.category=category;
                SearchConstraintActivity.this.finish();
            }
        });
    }
}